package edu.pitt.dbmi.odie.gapp.gwt.client.util;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.RecordList;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.FormLayoutType;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class ODIE_ClientUtils {
	
	public static void selectBasedOnData(ListGrid grid) {
		grid.deselectAllRecords() ;
		RecordList recordList = grid.getRecordList() ;
		 for (int idx = 0 ; idx < recordList.getLength() ; idx++) {
			 Record record = recordList.get(idx) ;
			 String isSelected = record.getAttribute("isSelected") ;
			 if (isSelected != null && isSelected.equals("true")) {
				 grid.selectRecord(idx) ;
			 }
		}
	}
	
	public static ListGrid buildListGrid() {
		ListGrid grid = new ListGrid();
		grid.setWidth100();
		grid.setHeight100();
		grid.setAutoFetchData(true);
		grid.setAlternateRecordStyles(true);
		grid.setShowAllRecords(true);
		grid.setSelectionType(SelectionStyle.SINGLE);
		grid.setCanSort(true) ;
		return grid;
	}
	
	public static HLayout buildHGroup(String groupName) {

		HLayout group = new HLayout();
		group.setMembersMargin(5);
		group.setLayoutMargin(5);
		group.setIsGroup(true);
		group.setGroupTitle(groupName);

		return group;
	}
	
	public static VLayout buildVGroup(String groupName) {

		VLayout group = new VLayout();
		group.setLayoutMargin(5);
		group.setMembersMargin(5);
		group.setIsGroup(true);
		group.setGroupTitle(groupName);

		return group;
	}
	
	public static VLayout buildVGroup(String groupName, DataSource dataSource, RecordDoubleClickHandler doubleClickHandler, String sortField) {

		VLayout group = new VLayout();
		group.setLayoutMargin(5);
		group.setMembersMargin(5);
		group.setIsGroup(true);
		group.setGroupTitle(groupName);

		if (dataSource != null) {
			ListGrid listGrid = null;
			listGrid = ODIE_ClientUtils.buildListGrid();
			listGrid.setDataSource(dataSource);
			if (doubleClickHandler != null) {
				listGrid
				.addRecordDoubleClickHandler(doubleClickHandler);
			}
			if (sortField != null) {
				listGrid.setSortField(sortField) ;
			}
			group.addMember(listGrid);
		}

		return group;
	}
	
	public static ButtonItem buildButtonItem(String name, ClickHandler clickHandler) {
		ButtonItem buttonItem = buildButtonItem(name) ;
		buttonItem.addClickHandler(clickHandler) ;
		return buttonItem;
	}
	
	public static ButtonItem buildButtonItem(String name) {
		ButtonItem buttonItem = new ButtonItem(name);
		buttonItem.setHeight(20);
		buttonItem.setWidth(150);
		buttonItem.setColSpan(1);
		buttonItem.setStartRow(false);
		buttonItem.setEndRow(false);
		buttonItem.setAlign(Alignment.CENTER);
		return buttonItem;
	}
	
	public static DynamicForm buildButtonGroup(String title, ButtonItem[] buttonItems) {

		DynamicForm form = new DynamicForm();

		form.setGroupTitle(title);
		form.setIsGroup(true);

		form.setCellBorder(0);
		form.setCellPadding(2);
		form.setCellSpacing(0);
		form.setAlign(Alignment.CENTER);
		form.setMinColWidth(1);
		form.setFixedColWidths(false);
		form.setTitleWidth(0);
		form.setItemLayout(FormLayoutType.TABLE);
		form.setNumCols(buttonItems.length);

		form.setWidth100();

		int[] columnWidths = new int[buttonItems.length];
		for (int idx = 0; idx < columnWidths.length; idx++) {
			columnWidths[idx] = 150;
		}
		form.setColWidths(columnWidths);
		form.setFields(buttonItems);

		return form;

	}

	
}
